/*
 * sgc_chat_connections.h
 *
 *  Created on: 2011-7-11
 *      Author: rendong
 */

#ifndef SGC_CHAT_CONNECTIONS_H_
#define SGC_CHAT_CONNECTIONS_H_

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include<sys/epoll.h>
#include <sys/types.h>

#include "../core/sgc_config.h"
#include "../core/sgc_list.h"
#include "../core/sgc_log.h"
#include "../core/sgc_hash.h"
#include "../core/sgc_inthash.h"
#include "../os/sgc_epoll.h"

#define PKG_TYPE_SIZE 2
#define PKG_LEN_SIZE 4
#define PKG_HEAD_SIZE 6

#define CHANNEL_WORLD "world"

#define LOGIN_TYPE_NOLOGIN 0
#define LOGIN_TYPE_USER 1
#define LOGIN_TYPE_ADMIN 2

// 定义全局变量
extern sgc_inthash_table_t * sgc_all_sessions; //  fd => session
extern sgc_hash_table_t * sgc_all_userids; // userid => session
extern sgc_hash_table_t * sgc_all_channels; // channelname => list<session>

// 类型定义
typedef struct _sgc_chat_session_s{
	int fd;
	struct sockaddr_in *clientaddr;
	sgc_epoll_socketserver_config_t const * config;

	char * buffer;
	int bufferpos;
	int buffersize;

	char login_type; // 登录类型， 0：未登录 1：普通用户 2：管理员
	char * userid;	// 用户uid唯一
	char * username; // 用户名

	int count;	// 发送消息数量

	sgc_list_t * channels; // 加入的频道列表

	sgc_list_t * friends; // 好友列表
}sgc_chat_session_t;


void chat_client_new_handler(int fd, struct sockaddr_in *clientaddr,  sgc_epoll_socketserver_config_t const* config);
void chat_client_message_handler(int fd, char const * buffer, int length);
void chat_client_close_hander(int fd);

/**
 *  分发具体消息
 */
void chat_message_dispatch(sgc_chat_session_t * session, int len,int type, char * message);

void sgc_start_chat_server();


#endif /* SGC_CHAT_CONNECTIONS_H_ */
